﻿# Sound.py: Alien Xenocide sound script
# Written by Victor Luna Freire - victorclf@live.com
# Copyright (c) Victor Luna Freire. All rights reserved.
# Sunday, June 24, 2009 00:00:00 PM
# Python 2.6.2
import os
import pygame

import Defines


class SoundManager:
    def __init__(self):
        self.DEFAULT_PATH = os.path.join(os.curdir, "data", "sounds")
        self.sfx = {}
        self.sound_enabled = Defines.SOUND
    
    def pre_init(self):
        pygame.mixer.pre_init(44100, -16, 2, 1024*4)
        

    def load_sounds(self):
        for root, dirs, files in os.walk(self.DEFAULT_PATH):
            for f in files:
                if f[-3:] == "wav":
                    try:
                        new_sound = pygame.mixer.Sound(os.path.join(root, f))
                    except: 
                        print "Error: failed to load - %s" % f
                        continue
                    sfxname = f[:-4]                    
                    self.sfx[sfxname] = new_sound
                    
                    
    def play_music(self, name):
        if not self.sound_enabled: return
        try:
            pygame.mixer.music.load(os.sep.join((self.DEFAULT_PATH, name + '.ogg')))
            pygame.mixer.music.play(-1)
        except:
            pass #print 'missing music: %s' % name
        
    
    def play_sound(self, name):
        if not self.sound_enabled: return
        if pygame.mixer and self.sound_enabled:
            try:
                self.sfx[name].play()
            except:
                print "Unable to play sound %s!" % name
                
                            

    def get_texture(self, name):
        try:
            return self.sprites_surfaces[name]
        except:
            print "Error: texture not found - %s" % name
            print self.sprites_surfaces
            raise SystemExit


Sound = SoundManager()

